$(function() {
	$('.footable').footable();
	$("tfoot tr td").show();
	if(jQuery.cookie('intro-room')!="true"){
        $(".unused").first().attr("data-step","1").attr("data-intro","选择时间").attr("data-position","right");
        $($(".unused").get(1)).attr("data-step","2").attr("data-intro","再次选择时间").attr("data-position","right");
        $("table").attr("data-step","3").attr("data-intro","完成快速预定").attr("data-position","right");
        introJs().start();
        jQuery.cookie('intro-room',"true",{expires:10000,path:"/"});
    }
	$("#selectdate").datepicker({
		dateFormat : 'yy-mm-dd',
		minDate : new Date(),
		beforeShow : function(input, instant) {
			setTimeout(function() {
				$('#ui-datepicker-div').css("z-index", 15);
			}, 100);
		}
	});
	$("tr").off("onmouseover");
	$("div").tooltip();
	$("#selectdate").change(
			function() {
				$.get("/meetings/use", {
					date : $("#selectdate").val()
				}, function(data) {
					var html = "";

					for ( var i = 0; i < data.length; i++) {
						var ev = data[i];
						var title = "";
						var meet = meetings[""+ev.meetingId];
						if(typeof meet != 'undefined'){
							if (meet.hasTv == 1) {
								title += "TV ";
							}
							if (meet.hasLync == 1) {
								title += "Lync ";
							}
							if (meet.hasPhone == 1) {
								title += "Phone ";
							}
							if (meet.hasIt == 1) {
								title += "IT ";
							}
						}
						var row = '<tr meetingId="' + ev.meetingId
								
								+ '">';
						row += "<td  title='" + title + "' class='" + title + "'>"+ev.meetingName+"</td>";
						row += "<td class='" + isUsed(ev.eight) + "' clock='8'>"
								+ "</td>";
						row += "<td class='" + isUsed(ev.eightHalf)
								+ " clock=8.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.nine) + "' clock='9'>"
								+ "</td>";
						row += "<td class='" + isUsed(ev.nineHalf)
								+ " clock=9.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.ten) + "' clock='10'>"
								+ "</td>";
						row += "<td class='" + isUsed(ev.tenHalf)
								+ "' clock='10.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.eleven)
								+ "' clock='11'>" + "</td>";
						row += "<td class='" + isUsed(ev.elevenHalf)
								+ "' clock='11.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.twelve)
								+ "' clock='12'>" + "</td>";
						row += "<td class='" + isUsed(ev.twelveHalf)
								+ "' clock='12.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.thirteen)
								+ "' clock='13'>" + "</td>";
						row += "<td class='" + isUsed(ev.thirteenHalf)
								+ "' clock='13.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.forteen)
								+ "' clock='14'>" + "</td>";
						row += "<td class='" + isUsed(ev.forteenHalf)
								+ "' clock='14.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.fifteen)
								+ "' clock='15'>" + "</td>";
						row += "<td class='" + isUsed(ev.fifteenHalf)
								+ "' clock='15.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.sixteen)
								+ "' clock='16'>" + "</td>";
						row += "<td class='" + isUsed(ev.sixteenHalf)
								+ " 'clock='16.5'>" + "</td>";
						row += "<td class='" + isUsed(ev.seventeen)
								+ "' clock='17'>" + "</td>";
						row += "<td class='" + isUsed(ev.seventeenHalf)
								+ "' clock='17.5'>" + "</td>";
						row += '</tr>';
						html += row;
					}

					$('table tbody').html(html).trigger('footable_initialize');
					$('.footable').footable();
					$("tfoot tr td").show();
				});
			});
	$(document).on("mouseover",".used",
			function() {
				if (typeof $(this).attr("title") == 'undefined') {
					var clock = $(this).attr("clock");
					var meetingId = $(this).parent().first().attr("meetingId");
					$.get("/meetings/use/ajax", {
						meetingId : meetingId,
						date : $("#selectdate").val(),
						clock : clock
					}, function(data) {
						$(this).attr(
								"title",
								"会议名称:" + data.meetingName + "<br/>会议标题:"
										+ data.meetingTitle + "<br/>开始时间:"
										+ data.bookBeginTime + "<br/>结束时间:"
										+ data.bookEndTime + "使用人:"
										+ data.workerId);
					});

				}
			});
	//话说jquery api变化也太快了吧
	$(document).on("click",".unused",
					function() {
						var size = $(".select").size();
						if (size <= 1) {
							if (size == 0) {
								$(this).addClass("select");
							} else {
								var tr=$(this).parent();
								if (tr.attr("meetingid") == $(".select").parent().attr("meetingid")) {
									var clock1 = $(this).attr("clock");
									var clock2 = $(".select").attr("clock");
									if (clock1 == clock2) {
										$(this).removeClass("select");
									} else {
										clock1 = parseFloat(clock1);
										clock2 = parseFloat(clock2);
										var clockMax = clock1;
										var clockMin = clock1;
										if (clock1 > clock2) {
											clockMin = clock2;
										} else {
											clockMax = clock2;
										}
										for ( var i = clockMin + 0.5; i < clockMax; i = i + 0.5) {
											if (tr.find('td[clock="' + i + '"]').hasClass("used")) {
												alert("哦哦，中间貌似有人订了哦，请重新选择！");
												return;
											}
										}
										$(this).addClass("select");
										var result = confirm("您是否确定预定会议室"
												+ $(this).parent().children()
														.first().text() + "时间"
												+ time(clockMin) + "~" +time(clockMax+0.5)
												+ "?");
										if (result) {
											  var selectdate=$("#selectdate").val();
											  var schdata = {
                                                      startdate : selectdate+" "+time(clockMin),
                                                      enddate : selectdate+" "+time(clockMax+0.5),
                                                      repweeks : 0,
                                                      meetingId : tr.attr("meetingid"),
                                                      title : "快速会议",
                                                      details : "快速会议"
                                                  };
											$.get("/meetings/book",schdata,function(data){
												$(".select").each(function() {
													$(this).removeClass("select");
												});
												if(data==false){
													alert("预定失败，请确定您选择的时间在现在之后！");
												}else{
													alert("预订成功！");
													for ( var i = clockMin ; i <= clockMax; i = i + 0.5) {
														tr.find('td[clock="' + i + '"]').addClass("used");
													}
												}
											});

										} else {
											$(".select").each(function() {
												$(this).removeClass("select");
											});
										}
									}

								} else {
									alert("您只可以选择同一个会议室的两个时间点!");
								}

							}

						} else {
							alert("您只可以选择同一个会议室的两个时间点!");
						}
					});
});
function isUsed(value) {
	if (value == 0) {
		return "unused";
	} else {
		return "used";
	}
}
function time(time){
	var int=parseInt(time);
	var min="00";
	var second="00";
	var hour=int;
	if(int!=time){
		min="30";
	}
	if(time<10){
		hour="0"+hour;
	}
	return hour+":"+min+":"+second;
}